<template>
    <div style="">
        <div v-for="(checkItem,index) in checkList"
             :key="index" style="position: relative;margin-bottom: 10px;">
            <receiving_materialcheck_with_seclist
                    :props_edit="edit"
                    :props_show_close="$props.props_show_close"
                    :props_show_close2="$props.props_show_close2"
                    :props_devel-material-check="checkItem"
                    :props_devel-material-item-dtl="checkItem?.materialItemDtl"
                    :props_devel-material-item="checkItem?.materialItem"
                    :props_secondary-list="checkItem?.secondaryList"
                    :props_billingType="formData.billingType"
                    :props_show_checkbox="false"
                    :props_activeName="$props.props_activeName"
                    @updateMoney="()=>{
                        $emit('updateMoney')
                        wrapper_action()

                    }"
            ></receiving_materialcheck_with_seclist>
        </div>

        <section  v-if="!$props?.props_hiddenFooter">
            <div style="display: flex;justify-content: space-between;"
                 v-if="!isHidden_billingType"
            >
                <span>暂未开单</span>
                <van-switch v-model="formData.billingType"
                            size="20px"
                            :active-value="5"
                            :inactive-value="1"
                />
            </div>
            <div style="display: flex;justify-content: space-between;margin-bottom: 10px;margin-top: 10px;">
                <span>总二次工艺费用</span>
                <strong>{{ toFixedInt(watch_secMoney) }}</strong>
            </div>
            <div style="display: flex;justify-content: space-between;">
                <span><i class="red" v-if="(watch_secMoney>0)&&(formData.billingType!=5)"
                         style="padding-right: 2px;">*</i>附件 </span>

                <uploadImagePlus :fileList="(attachList||[]).filter(item=>item.expendType=='工艺报销单据')"
                                 :uploadType="$uploadType.file_purchase"
                                 ref="uploadImageDom"
                                 @change="files=>{
                                     var attachlist_no=(attachList||[]).filter(item=>item.expendType!='工艺报销单据')
                                      attachList=(attachlist_no||[]).concat(files.map(item=>{
                                              item.expendType=`工艺报销单据`
                                              return  item
                                          }))
                           }"
                                 @deleteFile="(file)=>{
                              deleteFile_group(file)
                            }"
                           >
                </uploadImagePlus>

            </div>




          <div
              style="display: flex;align-items: flex-start;justify-content: space-between;margin-top: 5px;margin-top: 10px;"
              v-if="materialReceiving?.id">
            <span desc="未请款中的可请款的部分">可请款</span>
            <span style="font-size: 14px;">{{ toFixedInt(watch_sum_keyibaoxiao) }}</span>
          </div>

          <div
              style="display: flex;align-items: flex-start;justify-content: space-between;margin-top: 5px;margin-top: 10px;"
              v-if="materialReceiving?.id">
            <span>请款中</span>
            <span style="font-size: 14px;">{{ toFixedInt(watch_sum_paystate3) }}</span>
          </div>
          <div
              style="display: flex;align-items: flex-start;justify-content: space-between;margin-top: 5px;margin-top: 10px;"
              v-if="materialReceiving?.id">
            <span>已请款</span>
            <span style="font-size: 14px;">{{ toFixedInt(watch_sum_paystate10) }}</span>
          </div>
        </section>

    </div>
</template>
<script>
import receiving_materialcheck_with_seclist from './receiving_materialcheck_with_seclist.vue'
import {booleanDefault, deepClone, toFixedInt} from "@/utils";
import uploadFilePlusCom from "@/components/UploadImage/uploadFilePlus.vue";
import materialDtl_with_secItemCom
    from "@/views/material/dev/developer/developerproductlist/devList/materialDtl_with_secItemCom.vue";
import {lodashEmpty} from "@/utils/lodashUtil";
import { alertFail, alertWithPayload } from '@/utils/alertUtil';
import uploadImagePlus from "@/components/UploadImage/uploadImagePlus.vue";
import * as develMaterialApi from '@/api/material_dev';
import * as dev_material_receivingApi from '@/api/dev_material_receiving'


export default {
    components: {
        uploadImagePlus,
        materialDtl_with_secItemCom,
        uploadFilePlusCom,
        receiving_materialcheck_with_seclist
    },
    data() {
        return {
            toFixedInt,
            isvalidate: this.$props.props_isvalidate,
            watch_secMoney: 0,
            watch_courierMoney: 0,
            dataList: [],
            attachList: [],
            formData: {
                billingType: '1',
            },
            edit: this.$props.props_edit,
            checkList: [],
            materialReceiving: this.$props.props_materialReceiving,
            isHidden_billingType: false,
            //可报销金额
            watch_sum_keyibaoxiao: 0,
          watch_sum_paystate3: 0,
          watch_sum_paystate10: 0,


        }
    },
    props: {
        props_activeName: {
          type: [String],
        },
        props_show_close2: {
            type: [Boolean],
            default() {
                return false
            }
        },
        props_show_close: {
            type: [Boolean],
            default() {
                return false
            }
        },
        props_isvalidate: {
            type: [Boolean],
            default() {
                return false
            }

        },
        props_edit: {
            type: [Boolean], default() {
                return false;
            },
        },
        props_hiddenFooter:{
            type: [Boolean],
            default() {
                return false
            }
        }
    },

    methods: {
        initByDialog(_data) {
            this.checkList = _data?.checkList || [];
            console.log("-> this.checkList", this.checkList);
            this.edit = booleanDefault(_data?.edit, this.$props.props_edit)
            this.isvalidate = booleanDefault(_data?.isvalidate, this.$props.props_isvalidate)
            this.isHidden_billingType = booleanDefault(_data?.isHidden_billingType, this.$props?.isHidden_billingType)
            this.materialReceiving = _data?.materialReceiving || this.$props?.props_materialReceiving
          this.attachList = _data?.attachList || this.$props?.props_attachList||[]

            this.wrapper_action()
        },
      async deleteFile_group(file) {
        if (file && file.id) {
          var result=await dev_material_receivingApi.devmateraildelattachf(file && file.id);
          alertWithPayload(result)
        }

      },

        wrapper_action() {
            this.action_watch_secMoney()
            this.action_watch_courierMoney();
            this.action_watch_sum_keyibaoxiao();
            this.action_watch_sum_paystate3()
          this.action_watch_sum_paystate10()
        },
        //可报销 金额
        action_watch_sum_keyibaoxiao() {
            var _dataList = this.checkList || []
            var sum = 0;

            for (let i = 0; i < this.checkList.length; i++) {
                for (let j = 0; j < this.checkList[i].secondaryList?.length; j++) {
                    var processMoney = this.checkList[i].secondaryList[j].processMoney || 0
                    var courierMoney = this.checkList[i]?.secondaryList[j].courierMoney || 0
                    if (/*明细是可报销状态*/this.checkList[i]?.secondaryList[j]?.materialReceivingDtl?.payState <=1 &&
                        this.checkList[i]?.secondaryList[j]?.materialReceivingDtl?.expenseType==5/*可报销*/) {
                        sum += parseFloat(processMoney || 0) + parseFloat(courierMoney || 0)
                    }
                }
            }

            this.watch_sum_keyibaoxiao = sum
        },
      action_watch_sum_paystate3() {
        var _dataList = this.checkList || []
        var sum = 0;

        for (let i = 0; i < this.checkList.length; i++) {
          for (let j = 0; j < this.checkList[i].secondaryList?.length; j++) {
            var processMoney = this.checkList[i].secondaryList[j].processMoney || 0
            var courierMoney = this.checkList[i]?.secondaryList[j].courierMoney || 0
            if (/*明细是可报销状态*/this.checkList[i]?.secondaryList[j]?.materialReceivingDtl?.payState ==3 &&
            this.checkList[i]?.secondaryList[j]?.materialReceivingDtl?.expenseType==5/*可报销*/) {
              sum += parseFloat(processMoney || 0) + parseFloat(courierMoney || 0)
            }
          }
        }

        this.watch_sum_paystate3 = sum
      },
      action_watch_sum_paystate10() {
        var _dataList = this.checkList || []
        var sum = 0;

        for (let i = 0; i < this.checkList.length; i++) {
          for (let j = 0; j < this.checkList[i].secondaryList?.length; j++) {
            var processMoney = this.checkList[i].secondaryList[j].processMoney || 0
            var courierMoney = this.checkList[i]?.secondaryList[j].courierMoney || 0
            if (/*明细是可报销状态*/[5,10].includes(this.checkList[i]?.secondaryList[j]?.materialReceivingDtl?.payState||0)  &&
                this.checkList[i]?.secondaryList[j]?.materialReceivingDtl?.expenseType==5/*可报销*/) {
              sum += parseFloat(processMoney || 0) + parseFloat(courierMoney || 0)
            }
          }
        }

        this.watch_sum_paystate10 = sum
      },

        action_watch_secMoney() {
            var sum = 0
            for (let i = 0; i < this.checkList.length; i++) {
                for (let j = 0; j < this.checkList[i].secondaryList?.length; j++) {
                    var processMoney = this.checkList[i].secondaryList[j].processMoney || 0
                    //var courierMoney=this.dataList[i].secondaryList[j].courierMoney||0
                    sum += parseFloat(processMoney || 0)
                    //+parseFloat(courierMoney||0)
                }
            }
            this.watch_secMoney = sum
            return sum
        },
        action_watch_courierMoney() {
            var sum = 0
            for (let i = 0; i < this.checkList.length; i++) {
                for (let j = 0; j < this.checkList[i].secondaryList?.length; j++) {
                    var courierMoney = this.checkList[i].secondaryList[j].courierMoney || 0
                    //var courierMoney=this.dataList[i].secondaryList[j].courierMoney||0
                    sum += parseFloat(courierMoney || 0)
                    //+parseFloat(courierMoney||0)
                }
            }
            this.watch_courierMoney = sum
            return sum
        },
        // billing_type         tinyint      not null comment '是否缺单据：1 否 5 是',
        action_computed_billingType() {
            var billingType = 1;
            var ofValueSecList=[]
            for (let i = 0; i < this.checkList.length; i++) {
                for (let j = 0; j < this.checkList[i]?.secondaryList?.length; j++) {
                     this.checkList[i].secondaryList[j].processMoney>0&&ofValueSecList.push(this.checkList[i].secondaryList[j]);

                    var ignoreListName = ['固色', '洗水'];
                    if (!ignoreListName.includes(this.checkList[i].secondaryList?.[j].processName)) {
                        if (/*工艺金额*/  this.checkList[i].secondaryList?.[j].processMoney <= 0) {
                            console.log('退出 二次工艺 没哟金额');
                            return 5;
                        }
                        if (/*工艺金额*/this.checkList[i].secondaryList?.[j].processNum <= 0) {
                          console.log('退出 二次工艺 没有数量');
                          return 5;
                        }
                    }

                    if (this.checkList[i].secondaryList?.[j].courierMoney > 0) {
                        if (lodashEmpty(this.checkList[i].secondaryList?.[j].attachList || [])) {
                            console.log('退出 二次工艺没有专送附件');
                            return 5;
                        }
                    }
                }
            }


          if(ofValueSecList&&ofValueSecList.length>0){
                if (lodashEmpty(this.attachList.filter(item => item.expendType == '工艺报销单据'))) {
                  console.log('退出 工艺报销单据没有附件');
                  return 5;
                }
            }



            return 1;

        },
        async submitByDialog() {
            this.wrapper_action()


            if (lodashEmpty(this.checkList)) {
                alertFail('暂无数据');
                return false;
            }


            if (this.isvalidate) {
                if (this.formData?.billingType != 5) {
                    for (let i = 0; i < this.checkList.length; i++) {
                        for (let j = 0; j < this.checkList[i].secondaryList.length; j++) {
                            /*if (!this.checkList[i].secondaryList[j].processMoney) {
                                var ignoreListName = ['固色', '洗水']
                                if (!(ignoreListName.includes(this.checkList[i]?.secondaryList[j]?.processName))) {
                                    alertFail(`${this.checkList[i]?.secondaryList[j]?.processName}金额必填`);
                                    return false;
                                }
                            }*/

                            if (!this.checkList[i].secondaryList[j].processNum) {
                              var ignoreListName = ['固色', '洗水']
                              if (!(ignoreListName.includes(this.checkList[i]?.secondaryList[j]?.processName))) {
                                alertFail(`${this.checkList[i]?.secondaryList[j]?.processName}数量必填`);
                                return false;
                              }
                            }


                            if (this.checkList[i].secondaryList[j].courierMoney > 0) {
                                var attachList = this.checkList[i].secondaryList[j]?.attachList || [];
                                var attachListFilter = (attachList || []).filter(item => item.expendType == `${this.checkList[i].secondaryList[j]?.processName}二次工艺专送`);
                                if (lodashEmpty(attachListFilter)) {
                                    alertFail(`${this.checkList[i].secondaryList[j]?.processName}二次工艺专送 附件必填`);
                                    return false;
                                }
                            }
                        }
                    }

                    if (this.watch_secMoney > 0 && lodashEmpty(this.attachList)) {
                        alertFail('请上传附件')
                        return false
                    }
                }
            }


            var secondaryList = [];
            ;(this.checkList || []).map(item => {
                secondaryList = secondaryList.concat(item?.secondaryList || [])
            });

            if(!this.$props.props_hiddenFooter){
                if (lodashEmpty(secondaryList)) {
                    alertFail('没有工艺')
                    return false
                }
            }



            var params = {
                secondaryList: secondaryList,
                attachList: this.attachList || [],
                billingType: this.action_computed_billingType()
            };

            return {success: true, data: deepClone(params)}


        }

    }

}
</script>
